package com.shangrila.web.admin;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import com.shangrila.biz.domain.User;
import com.shangrila.web.common.SessionKeeper;
import com.shangrila.web.common.ShangrilaActionSupport;

@Controller
public class LoginAction extends ShangrilaActionSupport{

	private final static Logger log = LoggerFactory.getLogger(LoginAction.class);
	
	private User user = new User();
	
	public String input() throws Exception {
		Map<String, Object> session = getSession();
		
		if("kalman".equals(user.getUserNick()) && "nanjiang".equals(user.getPassword())){
			session.put(SessionKeeper.LOGIN_USER, user.getUserNick());
			log.info(getUserLoginInfo());
			return SUCCESS;
		}else{
			log.error(getUserLoginInfo());
			session.remove(SessionKeeper.LOGIN_USER);
			return INPUT;
		}
	}
	
	public String out() throws Exception {
		getSession().remove(SessionKeeper.LOGIN_USER);
		return SUCCESS;
	}
	
	
	private String getUserLoginInfo(){
		StringBuilder sb = new StringBuilder();
		sb.append("userNick:").append(user.getUserNick()).append(",userIpAddress:").append(getIpAddress());
		return sb.toString();
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
